/*
 * LoginView.java
 *
 * Created on Mar 21, 2010, 6:52:40 PM
 */
package assetl.desktop;

import assetl.system.AssetLControl;
import assetl.system.DataPacket;
import assetl.system.LoginPacket;
import java.awt.event.KeyEvent;
import resources.SecurityFunctions;

/**
 * Allows user to login to the application
 *
 * @author Devin Doman
 */
public class LoginView
   extends AssetView
{
   /**
    * Constructor for the LoginView
    *
    * @param pControl The controller for this view
    */
   public LoginView(AssetLControl pControl)
   {
      super(pControl);
      initComponents();
   }

   /** This method is called from within the constructor to
    * initialize the form.
    * WARNING: Do NOT modify this code. The content of this method is
    * always regenerated by the Form Editor.
    */
   @SuppressWarnings("unchecked")
   // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
   private void initComponents() {

      mLoginLbl = new javax.swing.JLabel();
      mPicLbl = new javax.swing.JLabel();
      mUserLbl = new javax.swing.JLabel();
      mPwrdLbl = new javax.swing.JLabel();
      mUserTxtFld = new javax.swing.JTextField();
      mPwrdTxtFld = new javax.swing.JPasswordField();
      mLogInBtn = new javax.swing.JButton();

      setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);

      mLoginLbl.setText("Log In");
      mLoginLbl.setName("mLoginLbl"); // NOI18N

      mPicLbl.setHorizontalAlignment(javax.swing.SwingConstants.CENTER);
      mPicLbl.setIcon(new javax.swing.ImageIcon(getClass().getResource("/assetl/desktop/resources/LightningTornado.jpg"))); // NOI18N
      mPicLbl.setName("mPicLbl"); // NOI18N

      mUserLbl.setText("User:");
      mUserLbl.setName("mUserLbl"); // NOI18N

      mPwrdLbl.setText("Password:");
      mPwrdLbl.setName("mPwrdLbl"); // NOI18N

      mUserTxtFld.setName("mUserTxtFld"); // NOI18N

      mPwrdTxtFld.setName("mPwrdTxtFld"); // NOI18N
      mPwrdTxtFld.addFocusListener(new java.awt.event.FocusAdapter() {
         public void focusGained(java.awt.event.FocusEvent evt) {
            mPwrdTxtFldFocusGained(evt);
         }
      });
      mPwrdTxtFld.addKeyListener(new java.awt.event.KeyAdapter() {
         public void keyPressed(java.awt.event.KeyEvent evt) {
            mPwrdTxtFldKeyPressed(evt);
         }
      });

      mLogInBtn.setText("Log In");
      mLogInBtn.setName("mLogInBtn"); // NOI18N

      javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
      getContentPane().setLayout(layout);
      layout.setHorizontalGroup(
         layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
         .addGroup(layout.createSequentialGroup()
            .addGap(33, 33, 33)
            .addComponent(mPicLbl, javax.swing.GroupLayout.PREFERRED_SIZE, 176, javax.swing.GroupLayout.PREFERRED_SIZE)
            .addGap(39, 39, 39)
            .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
               .addComponent(mPwrdLbl)
               .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                  .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 24, Short.MAX_VALUE)
                  .addComponent(mUserLbl)))
            .addGap(39, 39, 39)
            .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
               .addComponent(mLoginLbl)
               .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING, false)
                  .addComponent(mPwrdTxtFld, javax.swing.GroupLayout.Alignment.LEADING)
                  .addComponent(mUserTxtFld, javax.swing.GroupLayout.Alignment.LEADING, javax.swing.GroupLayout.DEFAULT_SIZE, 114, Short.MAX_VALUE)
                  .addComponent(mLogInBtn)))
            .addGap(34, 34, 34))
      );
      layout.setVerticalGroup(
         layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
         .addGroup(layout.createSequentialGroup()
            .addGap(23, 23, 23)
            .addComponent(mPicLbl, javax.swing.GroupLayout.PREFERRED_SIZE, 206, javax.swing.GroupLayout.PREFERRED_SIZE)
            .addContainerGap(36, Short.MAX_VALUE))
         .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
            .addGap(42, 42, 42)
            .addComponent(mLoginLbl)
            .addGap(52, 52, 52)
            .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
               .addComponent(mUserLbl)
               .addComponent(mUserTxtFld, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
            .addGap(39, 39, 39)
            .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
               .addComponent(mPwrdLbl)
               .addComponent(mPwrdTxtFld, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
            .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 29, Short.MAX_VALUE)
            .addComponent(mLogInBtn)
            .addGap(26, 26, 26))
      );

      pack();
   }// </editor-fold>//GEN-END:initComponents

    private void mPwrdTxtFldFocusGained(java.awt.event.FocusEvent evt) {//GEN-FIRST:event_mPwrdTxtFldFocusGained
       this.mPwrdTxtFld.enableInputMethods(true);
    }//GEN-LAST:event_mPwrdTxtFldFocusGained

    private void mPwrdTxtFldKeyPressed(java.awt.event.KeyEvent evt) {//GEN-FIRST:event_mPwrdTxtFldKeyPressed
       if (evt.getKeyCode() == KeyEvent.VK_ENTER)
       {
          mLogInBtn.doClick();
       }
    }//GEN-LAST:event_mPwrdTxtFldKeyPressed
   // Variables declaration - do not modify//GEN-BEGIN:variables
   private javax.swing.JButton mLogInBtn;
   /**
   * Login Label
   */
   private javax.swing.JLabel mLoginLbl;
   /**
   * Provides a location for a picture
   */
   private javax.swing.JLabel mPicLbl;
   /**
   * Label for password
   */
   private javax.swing.JLabel mPwrdLbl;
   /**
   * Password field for password
   */
   private javax.swing.JPasswordField mPwrdTxtFld;
   /**
   * Label for user name
   */
   private javax.swing.JLabel mUserLbl;
   /**
   * Text field for user name
   */
   private javax.swing.JTextField mUserTxtFld;
   // End of variables declaration//GEN-END:variables

   /**
    * Grabs all pertainent data from the fields and sets them in
    * a DataPacket object. This DataPacket is then returned by the
    * method.
    *
    * @param pFunction The function that needs the DataPacket
    * @return The DataPacket that has been set
    */
   public DataPacket grabDataPacket(String pFunction)
   {
      // Create and send a new LoginPacket
      String password = new String(mPwrdTxtFld.getPassword());
      try
      {
      mPacket = new LoginPacket(mUserTxtFld.getText(),
                  SecurityFunctions.byteArrayToHexString(
             SecurityFunctions.computeHash(password)));
      }
      catch(Exception e)
      {
         e.printStackTrace();
         System.err.println("Problem hashing the password.");
         mPacket = null;
      }
      return mPacket;
   }

   /**
    * Gets any needed data from the model
    */
   public void updateData()
   {
      // There is nothing to update for this view
   }

   /**
    * Enables functionality passed in for this view.
    */
   public void enable(String pFunction)
   {
      //Enable a FunctionListener for the button
      enable(pFunction, mLogInBtn, "Function");
   }

   /**
    * The entry point for this view
    */
   public void run()
   {
      hideMenuBar();
      enable("LogIn");
      // Hide the admin components
      setAdminComponents(3);
   }
}
